package com.eva.androidlibrary.api;

import android.content.Context;
import android.support.v4.util.ArrayMap;
import android.text.TextUtils;

import com.eva.androidlibrary.api.entity.Config;
import com.eva.androidlibrary.api.entity.ImageBean;
import com.eva.androidlibrary.api.entity.ResultData;
import com.eva.androidlibrary.api.entity.ShopInfo;
import com.eva.androidlibrary.api.entity.UserInfo;
import com.eva.androidlibrary.http.HttpRequest;

import java.io.File;

/**
 * @author eva
 * @date 16/4/29
 */
public class CommonApi extends BaseApi {

    protected final String hostUrl = "http://api.dev.qianguopai.com";

    public CommonApi(Context context) {
        super(context);
    }

    /**
     * 登录
     *
     * @param account
     *          账号,服务器会自动根据账号特征区分账号类型：手机账号、邮箱账号、普通账号
     * @param pwd
     *          密码，根据加密算法，把明文转为密文之后传递。
     * @param type
     *          1. 账号＋密码登陆
     *          2. 账号＋短信验证码登陆
     * @return
     */
    public void login(String account, String pwd, int type, HttpRequestCallback<UserInfo> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("account", account);
        params.put("pwd", encryptPassword(pwd));
        params.put("type", String.valueOf(type));
        post(UserInfo.class, hostUrl, "/1.0/user/login", params, callback);
    }

    /**
     * 登出
     *
     * @param token
     */
    public void logout(String token, HttpRequestCallback<ResultData> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        post(ResultData.class, hostUrl, "/1.0/user/logout", params, callback);
    }

    /**
     * 注册
     *
     * @param account
     *          必要参数，账号,服务器会自动根据账号特征区分账号类型：手机账号、邮箱账号、普通账号
     * @param pwd
     *          必要参数，密码
     * @param nickname
     *          非必要参数，会员昵称，不传或者传空则服务器根据账号类型自动取前3位后2位中间打***作为昵称，如137***24, cjs***om
     * @param type
     *          必要参数，验证码类型：1图片验证码，2短信验证码
     * @param captcha
     *          必要参数，输入图片验证码或者短信验证码，由type参数决定
     * @param token
     *          非必要参数，当type参数=1时则需要传这个参数且是未登录的token，获取未登录的接口
     * @param recommend
     *          非必要参数，推荐人，可选传项，用户输入的邀请码
     * @param shopId
     *          非必要参数，选择要配送水果店id，只有当APP-TYPE请求头是courier才有效
     */
    public void register(String account, String pwd, String nickname, int type, String captcha, String token,
                         String recommend, String shopId, HttpRequestCallback<UserInfo> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("account", account);
        params.put("pwd", encryptPassword(pwd));
        if (!TextUtils.isEmpty(nickname)) {
            params.put("nickname", nickname);
        }
        params.put("type", String.valueOf(type));
        params.put("captcha", captcha);
        if (!TextUtils.isEmpty(token)) {
            params.put("token", token);
        }
        if (!TextUtils.isEmpty(recommend)) {
            params.put("recommend", recommend);
        }
        if (!TextUtils.isEmpty(shopId)) {
            params.put("shopid", shopId);
        }
        post(UserInfo.class, hostUrl, "1.0/user/register", params, callback);
    }

    /**
     * 获取用户信息
     *
     * @param token
     * @param callback
     */
    public void getProfile(String token, HttpRequestCallback<UserInfo> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        post(UserInfo.class, hostUrl, "/1.0/my/profile", params, callback);
    }

    /**
     * 发送短消息
     *
     * @param type
     * @param phone
     * @param token
     * @param callback
     */
    public void sendSMS(String type, String phone, String token, HttpRequestCallback<ResultData> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        if (!TextUtils.isEmpty(token)) {
            params.put("token", token);
        }
        params.put("type", type);
        params.put("phone", phone);
        post(ResultData.class, hostUrl, "/1.0/captcha/sendsms", params, callback);
    }

    /**
     * 验证短信验证码
     *
     * @param type
     * @param phone
     * @param authCode
     * @param token
     * @param callback
     */
    public void checkAuthCode(String type, String phone, String authCode, String token, HttpRequestCallback<ResultData> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        if (!TextUtils.isEmpty(token)) {
            params.put("token", token);
        }
        params.put("type", type);
        params.put("phone", phone);
        params.put("captcha", authCode);
        post(ResultData.class, hostUrl, "/1.0/captcha/check", params, callback);
    }

    /**
     * 重置密码
     *
     * @param phone
     * @param type
     * @param authCode
     * @param newPsw
     * @param callback
     */
    public void resetPassword(String phone, String type, String authCode, String newPsw, HttpRequestCallback<ResultData> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("type", type);
        params.put("phone", phone);
        params.put("authcode", authCode);
        params.put("newpwd", encryptPassword(newPsw));
        post(ResultData.class, hostUrl, "/1.0/user/forgetpassword", params, callback);
    }

    /**
     * 修改密码
     * @param token
     *          用户Token
     * @param oldPwd
     *          老密码
     * @param newPwd
     *          新密码
     * @param callback
     */
    public void modifyPwd(String token, String oldPwd, String newPwd, HttpRequestCallback<ResultData> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("oldpwd", encryptPassword(oldPwd));
        params.put("newpwd", encryptPassword(newPwd));
        post(ResultData.class, hostUrl, "/1.0/my/editpassword", params, callback);
    }

    /**
     * 修改昵称
     *
     * @param token
     * @param nickname
     * @param callback
     */
    public void modifyNickname(String token, String nickname, HttpRequestCallback<ResultData> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("nickname", nickname);
        post(ResultData.class, hostUrl, "/1.0/my/editnickname", params, callback);
    }

    /**
     * 上传图片
     *
     * @param token
     * @param file
     */
    public void uploadImg(String token, File file, HttpRequestCallback<ImageBean> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        post(ImageBean.class, hostUrl, "/1.0/upload/img", params, "img_file", file, callback);
    }

    /**
     * 修改头像
     *
     * @param token
     * @param url
     * @param callback
     */
    public void editAvatar(String token, String url, HttpRequestCallback<ResultData> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("avatar", url);
        post(ResultData.class, hostUrl, "/1.0/my/editavatar", params, callback);
    }

    /**
     * 获取通用配置
     *
     * @param callback
     */
    public void getCommonConfig(HttpRequestCallback<Config> callback) {
        get(Config.class, hostUrl, "/1.0/common/shop", null, callback);
    }

    /**
     * 获取店铺详情
     *
     * @param token
     * @param shopId
     * @param callback
     */
    public void getShopDetail(String token, String shopId, HttpRequestCallback<ShopInfo> callback) {
        ArrayMap<String, String> params = new ArrayMap<>();
        params.put("token", token);
        params.put("shopid", shopId);
        post(ShopInfo.class, hostUrl, "/1.0/shop_info/myget", params, callback);
    }



}
